﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Clients_Control_Default : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {

            LoadDataLeft();
            LoadDataRight();
            String strPage = "";
            try
            {
                strPage = Request.Params["Pro"].ToString().Trim();
            }
            catch
            {

            }
            lblContentTitle.Text = Constant.ContentTitle(strPage);
        }
    }
    private void LoadDataLeft()
    {
        using (DamiWebDataContext db = new DamiWebDataContext())
        {
            var query = from p in db.tbl_News_Langs
                        where p.tbl_New.idNew == p.idNew
                        && p.idLang == 1 && p.tbl_New.isTindacbiet == false && p.tbl_New.isAuto == false
                        orderby p.tbl_New.addedDate descending
                        select new
                        {
                            p.titleNew,
                            p.idNew,
                            p.tbl_New.imageNew,
                            p.tbl_New.addedDate,
                            p.bodyNew,
                            p.introNew
                        };
           // this.grdLeft.DataSource = query.Skip(0).Take(3);
          //  this.grdLeft.DataBind();
        }
    }

    private int Index(String str, String find)
    {
        int vt = -1;
        vt = str.IndexOf(find);
        return vt;
    }
    private void LoadDataRight()
    {

        using (DamiWebDataContext db = new DamiWebDataContext())
        {
            var query = from p in db.tbl_News_Langs
                        where p.tbl_New.idNew == p.idNew
                        && p.idLang == 1 && p.tbl_New.isTindacbiet == false && p.tbl_New.isAuto == true
                        orderby p.tbl_New.addedDate descending
                        select new
                        {
                            p.titleNew,
                            p.idNew,
                            p.tbl_New.imageNew,
                            p.tbl_New.addedDate,
                            p.bodyNew,
                            p.introNew,
                            p.tbl_New.isAuto
                        };

            List<News> list = new List<News>();
            int i = 0;
            String url = "";
            foreach (var c in query)
            {
                News news = new News();
                news.idNew = c.idNew;
                String strFind = "";// c.introNew;
                int begin, end;
                if (c.isAuto == true)
                {
                    strFind = c.introNew;

                    begin = Index(strFind, "src=\"http://");
                    end = Index(strFind, ".jpg");
                    if (begin != -1 && end > begin + 3)
                    {
                        strFind = strFind.Substring(begin + 5, end - begin - 1);
                    }

                }
                else
                {
                    strFind = Constant.ROOT_PATH + c.imageNew.ToString().Trim().Replace("~/", "");
                    strFind = strFind.Trim();
                }
                String strRece = c.introNew;
                begin = Index(strRece, "<a");
                end = Index(strRece, "</a>");
                if (end > begin)
                {
                    strRece = strRece.Substring(begin, end + 1);
                }
                news.imageNew = strFind;
                news.introNew = c.introNew.Replace(strRece, "");
                news.titleNew = c.titleNew;
                list.Add(news);
                i++;
                if (i == 6)
                    break;
            }

            this.grdRight.DataSource = list;//query.Skip(0).Take(3);
            this.grdRight.DataBind();
        }

    }
}